Crate unsafe_io[][src]

Expand description

Non-owning unsafe I/O

The main types are:

ResourcePosix-ish typeWindows typePlatform-independent types
File/PipeRawFdRawHandleUnsafeHandle or UnsafeFile
SocketRawFdRawSocketUnsafeHandle or UnsafeSocket
AnyRawFdRawHandleOrSocketUnsafeHandle

and the main traits are:

TypeAs traitInto traitFrom trait
RawFdAsRawFdIntoRawFdFromRawFd
RawHandleAsRawHandleIntoRawHandleFromRawHandle
RawSocketAsRawSocketIntoRawSocketFromRawSocket
RawHandleOrSocketAsRawHandleOrSocketIntoRawHandleOrSocketFromRawHandleOrSocket
UnsafeFileAsUnsafeFileIntoUnsafeFileFromUnsafeFile
UnsafeSocketAsUnsafeSocketIntoUnsafeSocketFromUnsafeSocket
UnsafeHandleAsUnsafeHandleIntoUnsafeHandleFromUnsafeHandle

A brief explanation of the naming convention:

“Raw” is for platform-specific types and traits, such as std’s RawFd, AsRawFd, RawHandle, AsRawHandle, and similar, as well as unsafe-io’s RawHandleOrSocket, AsRawHandleOrSocket, and similar. “Handle” in this context means a Windows HANDLE.

“Unsafe” is for minimal platform-independent abstractions on top of the platform-specific types, such as UnsafeHandle (abstracts over RawFd and RawHandleOrSocket), UnsafeFile (abstracts over RawFd and RawHandle), and UnsafeSocket (abstracts over RawFd and RawSocket). “Handle” in this context means any kind of I/O handle.

Ownership Guarantees

Update: The approach to ownership in this crate is expected to be superceded by the io-lifetimes crate, which provides a much simpler and safer API.

The AsUnsafe* and IntoUnsafe* trait functions return non-owning handles, however these traits do require types that implement them to guarantee that they own the handles. This differs from their AsRaw* and IntoRaw* counterparts, which do not require such a guarantee. This crate defines an OwnsRaw trait which is unsafe to implement and which allows types to declare that they own the handles they hold, allowing them to opt into the blanket AsUnsafe* and IntoUnsafe* implementations. See rust-lang/rust#76969 for further background.

Additional Utilities

This crates also defines several additional utilities:

Examples

To use the AsUnsafeHandle trait:

use unsafe_io::AsUnsafeHandle;

// Open a normal file.
let file = std::fs::File::open("Cargo.toml").unwrap();

// Extract the handle.
let unsafe_handle = file.as_unsafe_handle();

Many types implement AsUnsafeHandle, however very few types implement FromUnsafeHandle. Most types implement only FromUnsafeFile or FromUnsafeSocket instead. For an example that extracts a handle and constructs a new file, we use the IntoUnsafeFile and AsUnsafeFile traits:

use unsafe_io::{FromUnsafeFile, IntoUnsafeFile};

// Open a normal file.
let file = std::fs::File::open("Cargo.toml").unwrap();

// Consume the file, returning the handle.
let unsafe_file = file.into_unsafe_file();

// Construct a new file with the handle.
let file = unsafe { std::fs::File::from_unsafe_file(unsafe_file) };

To implement the AsUnsafeHandle trait for a type, implement the AsRawFd trait for Posix-ish platforms and the AsRawHandleOrSocket trait for Windows platforms, and then implement OwnsRaw:

#[cfg(not(windows))]
use unsafe_io::os::rsix::{AsRawFd, RawFd};
#[cfg(windows)]
use unsafe_io::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
use unsafe_io::OwnsRaw;

struct MyType(std::fs::File);

#[cfg(not(windows))]
impl AsRawFd for MyType {
    fn as_raw_fd(&self) -> RawFd {
        self.0.as_raw_fd()
    }
}

#[cfg(windows)]
impl AsRawHandleOrSocket for MyType {
    fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
        self.0.as_raw_handle_or_socket()
    }
}

// Safety: `MyType` owns its handle.
unsafe impl OwnsRaw for MyType {}

This requires unsafe because implementing OwnsRaw asserts that the type owns its raw handle. See the char-device crate for a complete example.

Modules

OS-specific functionality.

Structs

Adapt an AsUnsafeReadWriteHandle implementation to implement AsUnsafeHandle with the read handle.

A non-owning unsafe I/O handle which on Windows is limited to handling what Windows considers to be RawHandles—mainly files and pipes.

A non-owning unsafe I/O handle.

A non-owning unsafe I/O handle that implements Read. Read functions are considered safe, so this type requires unsafe to construct.

A non-owning unsafe I/O handle which on Windows is limited to handling what Windows considers to be RawSockets—mainly TCP streams and listeners and UDP sockets.

A non-owning unsafe I/O handle that implements Write. Write functions considered are safe, so this type requires unsafe to construct.

A non-owning view of a resource which dereferences to a &Target or &mut Target.

Adapt an AsUnsafeReadWriteHandle implementation to implement AsUnsafeHandle with the write handle.

Traits

Portability abstraction over AsFd and AsHandleOrSocket.

Portability abstraction over AsFd and AsHandleOrSocket.

Portability abstraction over AsReadWriteFd and AsReadWriteHandleOrSocket.

Portability abstraction over AsReadWriteFd and AsReadWriteHandleOrSocket.

A trait for types which contain an unsafe file and can expose it.

A trait for types which contain an unsafe handle and can expose it.

Like AsUnsafeHandle, but for types which may have one or two handles, for reading and writing.

A trait for types which contain an unsafe socket and can expose it.

Portability abstraction over FromFd and FromHandleOrSocket.

Portability abstraction over FromFd and FromHandleOrSocket.

A trait for types which can be constructed from unsafe files.

A trait for types which can be constructed from an unsafe handle.

A trait for types which can be constructed from unsafe sockets.

Portability abstraction over IntoFd and IntoHandleOrSocket.

Portability abstraction over IntoFd and IntoHandleOrSocket.

A trait for types which can be converted into unsafe files.

A trait for types which can be converted into an unsafe handle.

A trait for types which can be converted into unsafe sockets.

Assert that a type owns its raw file descriptor or handle.

Type Definitions

Portability abstraction over BorrowedFd and BorrowedHandleOrSocket.

Portability abstraction over OwnedFd and OwnedHandleOrSocket.

Portability abstraction over RawFd and RawHandleOrSocket.